/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package LPlayerUILib;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.Locale;
import javax.swing.ImageIcon;

/**
 *
 * @author wcss
 */
public class JImageButton extends javax.swing.JPanel {

    public ImageIcon releasedImage;
    public ImageIcon pressedImage;
    public ImageIcon overImage;
    public ImageIcon currentImage;
    public Boolean enabledSwitchOverImage;
    public Boolean enabledSwitchPressImage;
    public String buttonText;
    public Boolean isButtonClicked = false;

    /**
     * Creates new form JImageButton
     */
    public JImageButton() {
        initComponents();
    }

    public JImageButton(String relImage, String preImage, String oveImage, String showTxt, Boolean switchOverImage, Boolean switchPressImage) {
        initComponents();
        initImageButton(preImage, relImage, oveImage);
        this.enabledSwitchOverImage = switchOverImage;
        this.enabledSwitchPressImage = switchPressImage;
        this.buttonText = showTxt;
    }

    public void initImageButton(String pressImage, String releaseImage, String oveImage) {
        this.releasedImage = new ImageIcon(JImageButton.class.getResource(releaseImage));
        this.pressedImage = new ImageIcon(JImageButton.class.getResource(pressImage));
        this.overImage = new ImageIcon(JImageButton.class.getResource(oveImage));
        this.setBackground(null);
        this.currentImage = this.releasedImage;
    }

    protected void paintComponent(Graphics g) {
        if (currentImage != null) 
        {
            Graphics2D g2d = (Graphics2D)g;
            Image img = currentImage.getImage();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.clearRect(0,0, this.getWidth(),this.getHeight());
            g2d.setColor(this.getBackground());
            g2d.fillRect(0, 0, this.getWidth(), this.getHeight());   
            g2d.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
            if (this.buttonText != null && !this.buttonText.isEmpty()) {
                int paintx = 0;
                int painty = 0;
                FontMetrics FM;
                int Ascent, Descent;
                FM = g.getFontMetrics(); //取得FontMetrics类实体
                Ascent = FM.getAscent(); //取得Ascent
                Descent = FM.getDescent(); //取得Descent
                //Rectangle2D rect = g.getFontMetrics().s.getStringBounds(buttonText, g);
                paintx = (this.getWidth() - FM.stringWidth(buttonText)) / 2;
                painty = (this.getHeight() - (Ascent + Descent)) / 2 + Ascent;
                g2d.drawString(buttonText, paintx, painty);
            }
        } else {
            super.paintComponent(g);
        }
    }

    public void nowPaint()
    {        
        this.paintImmediately(0, 0, this.getWidth(), this.getHeight());
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(new java.awt.Color(79, 64, 50));
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                formMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                formMouseReleased(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                formMouseExited(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                formMouseEntered(evt);
            }
        });
        setLayout(null);
    }// </editor-fold>//GEN-END:initComponents

    public void processButtonPressed() {
        this.currentImage = this.pressedImage;
        this.nowPaint();
        this.isButtonClicked = true;
    }

    public void processButtonReleased() {
        this.currentImage = this.releasedImage;
        this.nowPaint();
        this.isButtonClicked = false;
    }

    private void formMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMousePressed
        // TODO add your handling code here:
        processButtonPressed();
    }//GEN-LAST:event_formMousePressed

    private void formMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseReleased
        // TODO add your handling code here:
        if (this.enabledSwitchPressImage) {
            processButtonReleased();
        }
    }//GEN-LAST:event_formMouseReleased

    private void formMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseEntered
        // TODO add your handling code here:
        if (this.enabledSwitchOverImage) {
            this.currentImage = this.overImage;
            this.nowPaint();
        }
    }//GEN-LAST:event_formMouseEntered

    private void formMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseExited
        // TODO add your handling code here:
        if (this.enabledSwitchOverImage) {
            this.currentImage = this.releasedImage;
            this.nowPaint();
        }
    }//GEN-LAST:event_formMouseExited
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
